package ArrayList;

import java.util.Arrays;

public class Array<T> {

    //表示数组实际的长度
    private int size;
    //数组
    private T data[];

    //构造函数，capacity为数组的容量
    public Array(int capacity) {
        data = (T[]) (new Object[capacity]);
    }

    public Array(T []arr){
        data = (T[])(new Object[arr.length]);
        for(int i=0;i<arr.length;i++){
            addLast(arr[i]);
        }
    }

    //无参数的构造函数，默认的数组容量是10
    public Array() {
        this(10);
    }

    public int getSize(){
        return size;
    }

    //在数组末尾添加元素
    public void addLast(T e) {
        if (size >= data.length) {
            resize(2 * data.length);
        }
        data[size] = e;
        size++;

    }

    //在指定的位置插入元素
    public void addByIndex(int index, T e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException();
        }

        if (size >= data.length) {
            resize(2 * data.length);
        }

        for (int i = size; i > index; i--) {
            data[i] = data[i - 1];
        }

        data[index] = e;
        size++;

    }

    //删除末尾的元素
    public T removeLast() {
        if (size == 0) {
            throw new IllegalArgumentException();
        }
        T res = data[size - 1];
        size--;

        if (size < data.length / 4 && data.length / 2 != 0) {
            resize(data.length / 2);
        }

        return res;

    }

    //删除指定位置的元素
    public T removeByIndex(int index) {
        if (size == 0) {
            throw new IllegalArgumentException();
        }
        if (index < 0 || index > size) {
            throw new IllegalArgumentException();
        }

        T res = data[index];

        for (int i = index; i < size - 1; i++) {
            data[i] = data[i + 1];
        }
        size--;

        if (size < data.length / 4 && data.length / 2 != 0) {
            resize(data.length / 2);
        }

        return res;


    }

    //查找指定位置的元素
    public T findByIndex(int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException();
        }
        return data[index];
    }

    //查找元素的位置
    public int find(T e) {
        if (size == 0) {
            throw new IllegalArgumentException();
        }

        for (int i = 0; i < size; i++) {
            if (data[i] == e) {
                return i;
            }
        }

        return -1;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("数组的长度：");
        stringBuilder.append(size + "\t");
        stringBuilder.append("数组的容量：");
        stringBuilder.append(data.length);
        stringBuilder.append("\n[");
        for (int i = 0; i < size; i++) {
            stringBuilder.append(data[i] + ", ");
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }

    private void resize(int newCapacity) {
        T newData[] = (T[]) (new Object[newCapacity]);
        for (int i = 0; i < size; i++) {
            newData[i] = data[i];
        }

        data = newData;

    }

    public T get(int index){
        if(index < 0 || index >= size){
            throw new IllegalArgumentException("index is illegal");
        }
        return data[index];
    }

    public void swap(int i,int j){
        T t = data[i];
        data[i] = data[j];
        data[j] = t;
    }

    public T getFirst(){
        if(size == 0){
            throw new IllegalArgumentException("array is empty");
        }

        return data[0];
    }

    //修改方法
    public void set(int index,T e){
        data[index] = e;
    }
}
